package nxu.it.controller;

import io.undertow.util.StatusCodes;
import nxu.it.model.ApiResult;
import nxu.it.security.NeedLoginHandler;
import nxu.it.service.GreetingService;
import org.noear.solon.annotation.*;
import org.noear.solon.boot.web.MimeType;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.ModelAndView;
import static nxu.it.security.NeedLoginHandler.LOGIN_SESSION_FIELD;
import static nxu.it.security.NeedLoginHandler.REDIRECT_SESSION_FILED;

import java.util.HashMap;
import java.util.Map;

@Controller
public class MainController {
    @Inject
    GreetingService greetingService;

    @Mapping(value = "/", produces = MimeType.TEXT_HTML_VALUE)
    @Get
    @Before(NeedLoginHandler.class)
    public String index() {
        return "<h1>Yes, it works!</h1>";
    }

    @Mapping(value = "/hello", produces = MimeType.TEXT_HTML_VALUE)
    @Get
    @Before(NeedLoginHandler.class)
    public String hello(String name) {
        return greetingService.sayHello(name);
    }

    @Mapping(value = "/json", produces = MimeType.APPLICATION_JSON_UTF8_VALUE)
    @Get
    @Before(NeedLoginHandler.class)
    public ApiResult json() {
        return ApiResult.ok("请求成功");
    }

    @Mapping(value = "/render", produces = MimeType.TEXT_HTML_VALUE)
    @Get
    @Before(NeedLoginHandler.class)
    public ModelAndView render() {
        ModelAndView mv = new ModelAndView("test.ftl");
        mv.put("name", "张三");
        return mv;
    }
    @Mapping(value="/login",produces = MimeType.TEXT_HTML_VALUE)
    @Get
    public ModelAndView login(Context ctx){
        String username = ctx.session(LOGIN_SESSION_FIELD,String.class);
        ModelAndView mv = new ModelAndView("login.ftl");
        return mv;
//        if(username == null || username.isEmpty()){
//            return new ModelAndView("login.ftl");
//        }
//        else {
//            mv.view("welcome.ftl");
//            mv.put("username",username);
//        }
    }

    @Mapping(value = "/loginCheck",produces = MimeType.TEXT_HTML_VALUE)
    @Post
    public void loginCheck(String username, String password,Context ctx){

        if(username != password){
            ctx.output("用户名或密码错误");
        }
        ctx.sessionSet(LOGIN_SESSION_FIELD,username);
        String redirectUrl = ctx.session(REDIRECT_SESSION_FILED,String.class);
        if(redirectUrl != null){
            ctx.redirect(redirectUrl,303);
        }
        else{
            ctx.output("登录成功");
        }
//        if(username == null || username.equals(""))
//            ctx.output("用户名不能为空");
//        if(password == null || password.equals(""))
//            ctx.output("密码不能为空");
//        if(username.equals(password)){
//            ctx.sessionSet(LOGIN_SESSION_FIELD,username);
//            String redirectUrl = ctx.session(LOGIN_SESSION_FIELD,String.class);
//            if(redirectUrl != null){
//                ctx.redirect(redirectUrl,303);
//            }else{
//                ctx.output("登录成功");
//            }
//        }
//        else{
//            ctx.output("登录失败");
//        }
    }
}